package com.yzy.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.yzy.entity.User;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @description token 业务层
 * @author mgl
 * @date 2023/7/9 09:57
 * @version 1.0
 */
@Service("TokenService")
public class TokenService {

    public String getToken(User user) {
        Date start = new Date();
//        long currentTime = System.currentTimeMillis() + 10 * 1000;//10s有效时间
//        long currentTime = System.currentTimeMillis() + 60 * 60 * 1000;//一小时有效时间
        long currentTime = System.currentTimeMillis() + 60 * 60 * 1000 * 24 * 7;//七天有效时间
        Date end = new Date(currentTime);
        String token = "";

        token = JWT.create().withAudience(user.getId().toString()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }
}